package de.tud.binpacking.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import de.tud.binpacking.instance.Instance;

public class Order implements Comparable<Order>  {
	
	private int id;
	private int lastAisle = 0;
	
	private List<Item> positions;
	private Set<Integer> aisles;
	
	public Order(Instance instance) {
		positions = new ArrayList<Item>(instance.getCapacity());
		aisles = new HashSet<Integer>(instance.getNoAisles());
	}
	
	public int getId() {
		return id;
	}
	
	public void setItems(List<Item> items) {
		this.positions.addAll(items);
		
		for(Item curPosition : this.positions){
			aisles.add(curPosition.getAisle());
		}
		
		Collections.sort(this.positions);
		
		for (int aisle : aisles) {
			if (aisle > lastAisle) {
				lastAisle = aisle;
			}
		}
	}
	
	public void setId(int id) {
		this.id = id;
	}

	public List<Item> getItems() {
		return positions;
	}
	
	public int size() {
		return positions.size();
	}
	
	public Set<Integer> getAisles() {
		return aisles;
	}
	
	public int getLastAisle() {
		return lastAisle;
	}

	/**
	 * Sortierung nach Anzahl der Items
	 */
	@Override
	public int compareTo(Order arg) {
		if (this.size() < arg.size()) {
			return -1;
		} else if (this.size() > arg.size()) {
			return 1;
		} else {
			return 0;
		}
	}
	
	@Override
	public String toString() {
		String items = new String();
		
		for(Item item : this.positions){
			items += item + " ";
		}
		return "Order " + id + " number of articles " + positions.size() + " {" + items + "}";
	}

}
